﻿#include "Map.h"
#include "ShareComponents.h"
#include "StaticObject.h"
#include "FWGraphic.h"
#include "Camera.h"
#include "Box.h"
#include "RSManager.h"
#include "Sprite.h"
#include "Bridge.h"
#include "GameDefine.h"
#include "Sniper.h"
#include "StandSniper.h"
#include "DefenseCannon.h"
#include "WallTurret.h"
#include "Cannon.h"
#include "Object.h"
#include "Boss.h"
#include "ScubaSoldier.h"
#include "Player.h"
#include "AntiGravityShutter.h"
#include "Fire.h"
#include "Rock.h"
#include "Mechanical.h"
#include "BackGround.h"
#include "Barracks.h"
#include "Grotto.h"
#include "Platform.h"
#include "Capsule.h"
#include "Sensor.h"
#include "Gunner.h"
#include "Artillery.h"

Map::Map(SPRITE_ID id)
{	
	_objName = "";
	_sMap = RSManager::getInstance()->getSprite(id);


	switch (id)
	{
	case MAP1:
		readMap("Resources\\MAP\\map1.txt");
		_bg = new BackGround(1);
		_soundTrack = RSManager::getInstance()->getSoundTrack(MAP1_SOUNDTRACK);
		break;
	case MAP3:
		readMap("Resources\\MAP\\map3.txt");
		_bg = new BackGround(3);
		_soundTrack = RSManager::getInstance()->getSoundTrack(MAP3_SOUNDTRACK);
		break;
	case MAP5:
		readMap("Resources\\MAP\\map5.txt");
		_bg = new BackGround(5);
		_soundTrack = RSManager::getInstance()->getSoundTrack(MAP5_SOUNDTRACK);
		break;
	default:
		break;
	}
}

Map::~Map(void)
{
}

void Map::readMap(string fileName)
{		
	ifstream _mapStream;	
	_mapStream.open(fileName, ios::in);	
	if(!_mapStream.bad())
	{
		_mapStream>>this->_width;
		_mapStream>>this->_height;
		_arrMap = new int*[this->_height];
		for(int i = 0; i < this->_height; i++)
		{
			_arrMap[i] = new int[this->_width];
		}
		for(int j = 0; j < this->_height; j++)
		{
			for(int i = 0; i < this->_width; i++)			
			{				
				_mapStream>>_arrMap[j][i];				
			}
		}
		int index = 0;
		D3DXVECTOR2 pos;
		D3DXVECTOR2 size;					
		while(true)
		{
			_mapStream>>_objName;			
			if(_objName == "END")
			{
				_mapStream>>_endOfWorld.x;
				_mapStream>>_endOfWorld.y;
				break;
			}
			_mapStream>>_numObj;										
			if(_objName == "GROUND")
			{				
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;																					
					_mapStream>>size.x;
					_mapStream>>size.y;												
					StaticObject* st;
					st = new StaticObject(new Box(pos, size, D3DXVECTOR2(0, 0)), GROUND);	
					_lStaticObj.push_back(st);		
				}
				continue;
			}				
			if(_objName == "WATER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					StaticObject* st;										
					D3DXVECTOR2 size;					
					_mapStream>>size.x;
					_mapStream>>size.y;						
					st = new StaticObject(new Box(pos, size, D3DXVECTOR2(0, 0)), WATER);	
					_lStaticObj.push_back(st);	
				}
				continue;
			}
			if(_objName == "BRIDGE")					
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					Bridge* bridge;
					Box* box;					
					pos.y -= 32;
					D3DXVECTOR2 size;
					size.x = 64;
					size.y = 48;
					box = new Box(pos, size, D3DXVECTOR2(0, 0));
					bridge = new Bridge(box);
					_lBridge.push_back(bridge);
				}
				continue;
			}
			if(_objName == "SNIPER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					Sniper* sniper;						
					pos.y += 16;					
					sniper = new Sniper(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
					sniper->init();
					_lObject.push_back(sniper);	
				}
				continue;
			}
			if(_objName == "STANDSNIPER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					StandSniper* standSniper;						
					pos.y += 16;					
					standSniper = new StandSniper(new Box(pos, D3DXVECTOR2(48, 78), D3DXVECTOR2(0, 0)));
					standSniper->init();
					_lObject.push_back(standSniper);	
				}
				continue;
			}
			if(_objName == "WALLTURRET")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					WallTurret* wallTurret;						
					wallTurret = new WallTurret(new Box(pos, D3DXVECTOR2(64, 64), D3DXVECTOR2(0, 0)));
					wallTurret->init();
					_lObject.push_back(wallTurret);
				}
				continue;
			}
			if(_objName == "CANNON")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					Cannon* cannon;						
					cannon = new Cannon(new Box(pos, D3DXVECTOR2(66, 64), D3DXVECTOR2(0, 0)));
					cannon->init();
					_lObject.push_back(cannon);	
				}
				continue;
			}
			if(_objName == "DEFENSECANNON")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					DefenseCannon* def;											
					def = new DefenseCannon(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
					def->init(index++);
					_lObject.push_back(def);	
				}
				continue;
			}
			if(_objName == "BOSS1")
			{							
				_mapStream>>pos.x;					
				_mapStream>>pos.y;
				Boss* b;						
				pos.x += 16;					
				b = new Boss(new Box(pos, D3DXVECTOR2(66, 64), D3DXVECTOR2(0, 0)));		
				_lObject.push_back(b);				
				continue;
			}
			if (_objName == "BOSS3")
			{
				_mapStream>>pos.x;					
				_mapStream>>pos.y;
				Mechanical* m;										
				m = new Mechanical(new Box(pos, D3DXVECTOR2(500, 400), D3DXVECTOR2(0, 0)));		
				_lObject.push_back(m);	
				continue;
			}
			if (_objName == "BOSS5")
			{												
				_mapStream>>pos.x;
				_mapStream>>pos.y;
				AntiGravityShutter* shutter;
				shutter = new AntiGravityShutter(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
				_lObject.push_back(shutter);			
				continue;
			}
			if (_objName == "SCUBASOLDIER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					ScubaSoldier* scuba;					
					scuba = new ScubaSoldier(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
					_lObject.push_back(scuba);	
				}
				continue;
			}				
			if(_objName == "PLAYER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					_lPlayer.push_back(pos);
				}
				continue;
			}
			if(_objName == "BARRACKS")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					Barracks* br = new Barracks(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
					_lObject.push_back(br);
				}
				continue;
			}		
			if (_objName == "GUNNER")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					Gunner* gunner;					
					pos.y += 16;
					gunner = new Gunner(new Box(pos, D3DXVECTOR2(64, 64), D3DXVECTOR2(0, 0)));
					gunner->init();
					_lObject.push_back(gunner);	
				}
				continue;
			}
			if (_objName == "FGIFT")
			{
				int objType;
				for (int i = 0; i < _numObj; i++)
				{
					_mapStream>>objType;
					_mapStream>>pos.x;
					_mapStream>>pos.y;
					Capsule* c = new Capsule(new Box(pos, D3DXVECTOR2(32, 32), D3DXVECTOR2(0, 0)), (BULLET_ID)objType);
					_lObject.push_back(c);
					c->init();
				}				
				continue;
			}
			if (_objName == "SGIFT")
			{
				int objType;
				for (int i = 0; i < _numObj; i++)
				{
					_mapStream>>objType;
					_mapStream>>pos.x;
					_mapStream>>pos.y;
					Sensor* sensor = new Sensor(new Box(pos, D3DXVECTOR2(32, 32), D3DXVECTOR2(0, 0)), (BULLET_ID)objType);
					sensor->init();
					_lObject.push_back(sensor);
				}
				continue;
			}
			if (_objName == "PLATFORM")
			{				
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;					
					_mapStream>>size.x;
					_mapStream>>size.y;
					Platform* f = new Platform(new Box(pos, size, D3DXVECTOR2(0, 0)));	
					f->init();
					_lStaticObj.push_back(f);	
				}
				continue;
			}
			if (_objName == "FIRE")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;
					pos.y += 16;
					Fire* fire;					
					fire = new Fire(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));
					fire->init();
					_lObject.push_back(fire);	
				}
				continue;
			}
			if (_objName == "ROCK")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;					
					Grotto* rock;
					rock = new Grotto(new Box(pos, D3DXVECTOR2(0, 0), D3DXVECTOR2(0, 0)));					
					_lObject.push_back(rock);	
				}
				continue;
			}
			if (_objName == "ARMOREDVEHICLE")
			{
				continue;
			}
			if (_objName == "ARTILLERY")
			{
				for(int i = 0; i < _numObj; i++)
				{					
					_mapStream>>pos.x;					
					_mapStream>>pos.y;					
					Artillery* art;
					art = new Artillery(new Box(pos, D3DXVECTOR2(32, 32), D3DXVECTOR2(2, 3)));				
					_lObject.push_back(art);	
				}
				continue;
			}			
		}
		_mapStream.close();	
	}
}

void Map::render()
{
	int vp_left = gl_camera->getLeftSide() / 32;
	int vp_right = gl_camera->getRightSide() / 32;
	for(int i = vp_left; i < vp_right; i++)
	{
		for(int j = 0; j < this->_height; j++)
		{						
			_sMap->Reset(_arrMap[j][i]-1);
			_sMap->Render(D3DXVECTOR2(i * 32, (_height - j - 1) * 32));
		}
	}	
}

list<StaticObject*> Map::getStaticObjects()
{	
	return _lStaticObj;
}

void Map::resetMap(int i, int j, int frame)	
{
	_arrMap[i][j] = frame;
}
list<Bridge*> Map::getStaticBridge()
{	
	return _lBridge;
}

list<Object*> Map::getObject()
{	
	return _lObject;
}

int Map::getWidth()
{
	return _width;
}

int Map::getHeight()
{
	return _height;
}

list<D3DXVECTOR2> Map::getPlayer()
{
	return _lPlayer;
}

D3DXVECTOR2 Map::readObjectPosition(ifstream* stream)
{
	D3DXVECTOR2 pos;			
	*stream>>pos.x;
	*stream>>pos.y;			
	return pos;
}

BackGround* Map::getBackGround()
{
	return _bg;
}

FWDirectShow* Map::getSoundTrack()
{
	return _soundTrack;
}

D3DXVECTOR2 Map::getEndWorld()
{
	return _endOfWorld;
}
